home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Visual Basic 5.0 (2nd Edition) / Hardcore Visual Basic 5.0 - Second Edition (1997)(Microsoft Press).iso / Code / winany.bas < prev    next >
BASIC Source File  |  1997-06-14  |  4KB  |  139 lines

  1. Attribute VB_Name = "MWinFindAny"
  2. Option Explicit
  3.  
  4. '$ Uses DEBUG.BAS UTILITY.BAS WINTYPE.BAS
  5.  
  6. #If 1 Then
  7. Private Type TFindAny
  8.     sClass As String
  9.     sTitle As String
  10.     hWndTarget As Long
  11.     fCase As Boolean
  12. End Type
  13.  
  14. Function FindAnyWindow(Optional Title As String, _
  15.                        Optional Class As String, _
  16.                        Optional CaseSense As Boolean = True) As Long
  17.                        
  18.     ' Pass Title, Class, or both, but not neither
  19.     BugAssert Title <> sEmpty Or Class <> sEmpty
  20.     ' Store parameters in UDT
  21.     Dim find As TFindAny, f As Long
  22.     find.fCase = CaseSense
  23.     find.sClass = IIf(find.fCase, Class, UCase(Class))
  24.     find.sTitle = IIf(find.fCase, Title, UCase(Title))
  25.     ' Ask FindHelper to check each window
  26.     f = EnumChildWindows(GetDesktopWindow, AddressOf FindHelper, find)
  27.     FindAnyWindow = find.hWndTarget
  28.  
  29. End Function
  30.  
  31. Private Function FindHelper(ByVal hWnd As Long, find As TFindAny) As Long
  32.     
  33.     Dim sClass As String, sTitle As String
  34.     sClass = MWinTool.ClassNameFromWnd(hWnd)
  35.     sTitle = MWinTool.WindowTextFromWnd(hWnd)
  36.     If find.fCase = False Then
  37.         sTitle = UCase$(sTitle)
  38.         sClass = UCase$(sClass)
  39.     End If
  40.     If find.sTitle = sEmpty Then
  41.         If find.sClass = sEmpty Then
  42.             ' Can't both be empty
  43.             BugAssert True
  44.         Else
  45.             ' Only class needs to match
  46.             If sClass Like find.sClass Then
  47.                 find.hWndTarget = hWnd
  48.                 Exit Function
  49.             End If
  50.         End If
  51.     Else
  52.         If find.sClass = sEmpty Then
  53.             ' Only title needs to match
  54.             If sTitle Like find.sTitle Then
  55.                 find.hWndTarget = hWnd
  56.                 Exit Function
  57.             End If
  58.         Else
  59.             ' Both must match
  60.             If sTitle Like find.sTitle Then
  61.                 If sClass Like find.sClass Then
  62.                     find.hWndTarget = hWnd
  63.                     Exit Function
  64.                End If
  65.             End If
  66.         End If
  67.     End If
  68.     FindHelper = True
  69. End Function
  70.  
  71. #Else
  72. Private sClassFind As String, sTitleFind As String, fCase As Boolean
  73.  
  74. Function FindAnyWindow(Optional Title As String, _
  75.                        Optional Class As String, _
  76.                        Optional CaseSense As Boolean = True) As Long
  77.                        
  78.     Static fInFunc As Long
  79.     If fInFunc Then Exit Function
  80.     fInFunc = True
  81.     
  82.     ' Pass Title, Class, or both, but not neither
  83.     BugAssert Title <> sEmpty Or Class <> sEmpty
  84.     ' Store parameters in module-level variables
  85.     fCase = CaseSense
  86.     sClassFind = IIf(fCase, Class, UCase(Class))
  87.     sTitleFind = IIf(fCase, Title, UCase(Title))
  88.     Dim hWndTarget As Long, f As Long
  89.     ' Ask FindHelper to check each window
  90.     f = EnumChildWindows(GetDesktopWindow, AddressOf FindHelper, hWndTarget)
  91.     FindAnyWindow = hWndTarget
  92.     fInFunc = False
  93.  
  94. End Function
  95.  
  96. Private Function FindHelper(ByVal hWnd As Long, lParam As Long) As Long
  97.     
  98.     Dim sClass As String, sTitle As String
  99.     sClass = MWinTool.ClassNameFromWnd(hWnd)
  100.     sTitle = MWinTool.WindowTextFromWnd(hWnd)
  101.     If fCase = False Then
  102.         sTitle = UCase$(sTitle)
  103.         sClass = UCase$(sClass)
  104.     End If
  105.     If sTitleFind = sEmpty Then
  106.         If sClassFind = sEmpty Then
  107.             ' Can't both be empty
  108.             BugAssert True
  109.         Else
  110.             ' Only class needs to match
  111.             If sClass Like sClassFind Then
  112.                 lParam = hWnd
  113.                 Exit Function
  114.             End If
  115.         End If
  116.     Else
  117.         If sClassFind = sEmpty Then
  118.             ' Only title needs to match
  119.             If sTitle Like sTitleFind Then
  120.                 lParam = hWnd
  121.                 Exit Function
  122.             End If
  123.         Else
  124.             ' Both must match
  125.             If sTitle Like sTitleFind Then
  126.                 If sClass Like sClassFind Then
  127.                     lParam = hWnd
  128.                     Exit Function
  129.                End If
  130.             End If
  131.         End If
  132.     End If
  133.     FindHelper = True
  134. End Function
  135. #End If
  136. '
  137.  
  138.  
  139.